package com.example.mytips.createApi;

import org.apache.http.util.TextUtils;

import java.util.Arrays;
import java.util.List;

public class CreateApiUtil {

    public static String createApi(String notes, String funName, String url, String method, String bodyType, String entityName,String params) {

        String methodType = "1".equals(method) ? "ApiUtils.GetMethod" : "ApiUtils.PostMethd";

        String paramsStr = "";
        String paramsMap = "";
        if (!TextUtils.isEmpty(params)) {
            List<String> list = Arrays.stream(params.split(",")).toList();

            // 方法内参数
            StringBuilder builder = new StringBuilder();
            builder.append("{");
            for (String string : list) {
                builder.append("String? " + string + ",");
            }
            builder.append("}");
            paramsStr = builder.toString();

            // 给param put值
            StringBuilder builder2 = new StringBuilder();
            for (String string : list) {
                builder2.append(String.format(
                        "    if (!FFEmpty.stringIsEmpty(%s)) {\n" +
                        "      param[\"%s\"] = %s;\n" +
                        "    }",string,string,string));
                builder2.append("\n");
            }
            paramsMap = builder2.toString();

        }


        if ("1".equals(bodyType)) {
            String formattedString = String.format(
                    "  /**\n" +
                    "   *  %s\n" +
                    "   */\n" +
                    "  static Future<IFFR<T>> %s<T>(%s) async {\n" +
                    "    Map<String, dynamic> param = {};\n" +
                    "    %s\n" +
                    "    IFFR<T> r = await ApiUtils.methodAwait<T>(%s, \"%s\",param: param);\n" +
                    "    ApiUtils.handleIFFRProperties(r, parse: (data) {\n" +
                    "      return %s.fromJson(data);\n" +
                    "    });\n" +
                    "    return r;\n" +
                    "  }", notes, funName,paramsStr,paramsMap, methodType, url, entityName);
            return formattedString;
        } else if ("2".equals(bodyType)) {
            String formattedString = String.format("  /**\n" +
                    "   * %s\n" +
                    "   */\n" +
                    "  static Future<IFFR<L>> %s<L,E>(%s) async {\n" +
                    "    Map<String, dynamic> param = {};\n" +
                    "    %s\n" +
                    "    IFFR<L> r = await ApiUtils.methodAwait(%s, \"%s\",param: param);\n" +
                    "    ApiUtils.handleIFFRProperties(r, paging: true, limit: PAGELIMIT,\n" +
                    "     /*needParseData: (orig) {return ApiUtils.needParseData_plist(orig, \"lists\");},*/\n" +
                    "        parse: (data) {\n" +
                    "      return ApiUtils.handleIFFRList<E>(data, parseList: (item) {\n" +
                    "        return %s.fromJson(item);\n" +
                    "      });\n" +
                    "    });\n" +
                    "    return r;\n" +
                    "  }", notes, funName,paramsStr,paramsMap, methodType, url, entityName);

            return formattedString;
        }

        return "不支持的响应类型,暂时只支持1,2";

    }
}
